import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

def main_movie():
    print('开始抓取豆瓣年度高分电影榜单')
    opt = Options()
    opt.add_argument('--headless')
    opt.add_argument('--disable gpu')
    chrome = Chrome()
    url = 'https://movie.douban.com/annual/2021?source=navigation'
    chrome.get(url)
    f = open('10_movie.csv', mode='wt',encoding='utf-8')
    time.sleep(8)#等待网页加载
    #主页面抓取和跳转页面
    js="var q=document.documentElement.scrollTop=4700"#下拉滚动条
    chrome.execute_script(js)#用js的方式
    time.sleep(4)
    button=chrome.find_element(By.XPATH,'//*[@id="subjectList1317"]/div[3]/div/a/div[2]/div/div[1]/div[1]')
    f.write('1')
    f.write('\n')
    f.write(button.text)
    f.write(',')
    f.write(chrome.find_element(By.XPATH,'//*[@id="subjectList1317"]/div[3]/div/a/div[2]/div/div[2]/div[1]/div[1]').text)
    f.write(',')
    f.write('\n')
    button.click()
    time.sleep(1)

    listmovie=chrome.find_elements(By.XPATH,'//*[@id="subjectList1317"]/div[4]')
    # print(listmovie)
    time.sleep(3)
    for i in range(1):
        f.write(listmovie[i].text)
        f.write(',')
        f.write('\n')
        time.sleep(1)
    print('高分电影榜单抓取完毕')

    chrome.switch_to.window(chrome.window_handles[1])
    time.sleep(2)

    js1="var q=document.documentElement.scrollTop=1300"#下拉滚动条
    chrome.execute_script(js1)#用js1的方式
    time.sleep(3)
    # pinglun
    num = (chrome.find_element(By.XPATH, '//*[@id="comments-section"]/div[1]/h2/span/a').text)
    f3=f = open('movie_des.csv', mode='wt',encoding='utf-8')
    jianjie=chrome.find_element(By.XPATH,'//*[@id="info"]').text
    f3.write(jianjie)


    #tiaozhuan
    print('开始抓取热评')
    chrome.find_element(By.XPATH,'//*[@id="hot-comments"]/a').click()
    conment=chrome.find_elements(By.CLASS_NAME,'short ')

    f2=open('comment.csv',mode='wt',encoding='utf-8')
    for y in conment:
        f2.write(y.text)
        f2.write('\n')
        time.sleep(1)
    print('热评抓取完毕')
    print('爬虫运行完毕')

